const query = require('../db');
const util = require('../util');
module.exports = {
         //创建问卷
         addPaper(data,callback){
            let PK_PaperGuid =util.createGuid();
            let sql = `INSERT INTO paper (PK_PaperGuid,PaperName,Description,Status) VALUES ('${PK_PaperGuid}','${data.PaperName}','${data.Content}',1)`;
            query(sql,(err,data)=>{
                if(err){
                    util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                }else{
                    callback(PK_PaperGuid);
                }
            })
         },
         //获取所有的问卷或者指定Guid的问卷
         getPaper(data,callback){
             let sql  = `SELECT * FROM paper WHERE Status=1`
             if(data){
                sql = `SELECT * FROM paper WHERE PK_PaperGuid = '${data}' AND Status=1` ;
             }
             query(sql,(err,data)=>{
                 if(err){
                      util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                 }else{
                     callback(data);
                 }
             })
         },
         //删除指定GUID的问卷,并且返回删除的数据
         deletePaper(id,callback){
             let sql = `UPDATE paper SET Status = 0 WHERE PK_PaperGuid = '${id}'`;
             query(sql,(err,data)=>{
                  if(err){
                        util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                  }else{
                        let sql = `SELECT * FROM paper WHERE PK_PaperGuid = '${id}'`
                        query(sql,(err,data)=>{
                             if(err){
                                  util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                             }else{
                                 callback(data);
                             }
                        })
                  }
             })
         }
}